<template>

    <button @click="like">{{count}} {{text}}</button>

</template>

<script>
    export default {
        name: "TigLike", data: function () {
            return {
                like_status: false,
                count: 0,
            }
        }, methods: {
            like: function () {
                const that = this;

                axios.post(this.action).then(function (res) {
                    that.$toasted.success(that.like_status ?  '取消操作成功':'操作成功' );
                    that.like_status ? that.count-- : that.count++;
                    that.like_status = !that.like_status;
                }).catch(function (error) {
                    console.log(error.response);
                    switch (error.response.status) {
                        case 401:
                            that.$toasted.error('未登录');
                            break;
                        case 403:
                            that.$toasted.error('没有权限');
                            break;
                        case 404:
                            that.$toasted.error('路径不存在');
                            break;
                        case 409:
                            that.$toasted.error('不允许同时赞或踩');
                            break;
                        case 500:
                            that.$toasted.error('服务器错误');
                            break;
                        default:
                            that.$toasted.error('操作失败');
                    }
                })
            }
        }, computed: {
            text: function () {
                return this.like_status ? '已'+this.likeWord : this.likeWord
            }
        }, props: {
            action: {type: String, required: true},
            isLiked: {type: Boolean, default: false},
            likesCount: {type: Number, default: 0},
            likeWord: {type: String, default: '赞'},
        }, created: function () {
            console.log(this.likesCount);
            this.like_status = this.isLiked;
            this.count = this.likesCount;
        }
    }
</script>
